--
-- Author: mm
-- Date: 2018-03-29 11:15:52
--
-- NewguildDonateView
--

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.NewGuildDonateUI

local BtnNum = 3 -- 捐献按钮个数

function ClassRef:init()
	self._ui.Button_ok:addClickEventListener(function()
		executeFunc(self._onCloseView)
	end)
	self._isFirstDonate = false


	local onePart = tonumber( GD:getSystemPar( "guildDonateGoldOnePart", 1 ) ) -- 一份多少钱
	local addExpPart = tonumber( GD:getSystemPar( "guildDonateAddExpOnePart", 1 ) ) --一份多少经验

	local donateNum = {}
	local addExp = {}

	local partNum = tonumber( GD:getSystemPar( "guildDonateNormal", 1 ) )
	donateNum[1] = partNum*onePart
	addExp[1] = partNum*addExpPart
	self._ui.Panel_content:findChild( "Button_1/name"):setString( string.format( L("guild_DonateCard_gold"), donateNum[1] ) )
	self._ui.Panel_content:findChild( "Button_1/tips_point"):setString( string.format( L("guild_DonateCard_point"), addExp[1] ) )

	partNum = tonumber( GD:getSystemPar( "guildDonateMiddle", 3 ) )
	donateNum[2] = partNum*onePart
	addExp[2] = partNum*addExpPart
	self._ui.Panel_content:findChild( "Button_2/name"):setString( string.format( L("guild_DonateCard_gold"), donateNum[2] ) )
	self._ui.Panel_content:findChild( "Button_2/tips_point"):setString( string.format( L("guild_DonateCard_point"), addExp[2] ) )

	partNum = tonumber( GD:getSystemPar( "guildDonateSenior", 5 ) )
	donateNum[3] = partNum*onePart
	addExp[3] = partNum*addExpPart
	self._ui.Panel_content:findChild( "Button_3/name"):setString( string.format( L("guild_DonateCard_gold"), donateNum[3] ) )
	self._ui.Panel_content:findChild( "Button_3/tips_point"):setString( string.format( L("guild_DonateCard_point"), addExp[3] ) )

	local rewardItem = UD:parseRewardToBagItem(GD:getSystemStrPar("guildFirstDonateReward"))
	for i = 1, BtnNum do
		self._ui.Panel_content:getChildByName( "Button_" .. i ):addClickEventListener(function()
			local str
			if self._isFirstDonate then
				str = string.format(L("guild_FirstDonateReward"),donateNum[i],addExp[i],rewardItem:getAmount())
			else
				str = string.format(L("guild_OtherDonateReward"),donateNum[i],addExp[i])
			end

			local confirmDialog = display.newConfirmDialog(str,
				function() -- 确定
					executeFunc(self._onOperate,"donate", i)
				end)
			confirmDialog:setTitle(L("guild_Donate_Dsec"))
			UIHandler:addPopDialog( confirmDialog )
		end)
	end
end

-- dataTemp =  NewguildModel:getMyGuildMemberInfo
function ClassRef:refreshContent( dataTemp )
	local hadDonatedTimes = dataTemp:getDonatedTimes() -- 今日已捐次数
	local donateTimesMax = tonumber( GD:getSystemPar( "guildDonateTimesLimit", 3 ) ) -- 每日可捐献上限
	local lastDonateTimes = donateTimesMax - hadDonatedTimes -- 剩余可捐次数
	if lastDonateTimes < 0 then
		lastDonateTimes = 0
	end

	self._ui.tips:setString( string.format( L("guild_donate_times"), lastDonateTimes, donateTimesMax ) )

	local isLeader = dataTemp:isGuildLeader()
	self._isFirstDonate = not dataTemp:isHaveGetFirstDonateReward()
	--判断我是否是会长
	if not isLeader then
		for i = 1, BtnNum do
			display.setBtnTouchEnable( self._ui.Panel_content:getChildByName( "Button_" .. i ), 0 < lastDonateTimes )
		end
		self._ui.tips:setVisible(true)
	else
		self._ui.tips:setVisible(false)
	end

	local rewardItem = UD:parseRewardToBagItem(GD:getSystemStrPar("guildFirstDonateReward"))
	if rewardItem then
		if self._isFirstDonate then
			self._ui.tips_1:setString(string.format(L("guild_FirstDonate"),rewardItem:getAmount(),rewardItem:getName()))
		else
			self._ui.tips_1:setString(string.format(L("guild_FirstDonateDone"),rewardItem:getAmount(),rewardItem:getName()))
		end
	else
		dump("error guildFirstDonateReward config in globalpars")
	end


	self._ui.gold_num:setString(string.format(L("guild_owngold"),UD:getGold()))

end

return ClassRef
